set -e

pwd
# 为脚本赋权
chmod 755 ./sh/*.sh
chmod 777 ./config.xml

# 判断是否有传入版本，如无传入，默认为alpha
LIBRARY=$1
NEWVERSION=$2
# 提取上一次构建的版本号
WIDGET=$(grep 'eegrid' ./config.xml)

ID=$(echo $WIDGET|sed 's/<widget id="com.eegrid.efos.//g')

if [ ${ID:0:7} == app.alp ]
then
    LASTID=app.alpha
elif [ ${ID:0:7} == app.bet ]
then 
    LASTID=app.beta
else
    LASTID=app
fi

VERSION=$(echo $WIDGET|sed 's/<widget id="com.eegrid.efos.'$LASTID'" version="//g')
echo "默认上次版本号：${VERSION:0:5}"

if [ $NEWVERSION ]
then
    echo "有传入版本号，版本号为$NEWVERSION"
    THISVERSION=$NEWVERSION 
else
    echo "无传入版本号，使用默认上一次版本号"
    THISVERSION=${VERSION:0:5}
fi

if [ $LIBRARY ]
then
  echo "传入参数字符串不为空,参数为$LIBRARY"
else
  echo "传入参数字符串为空，转换为alpha"
  LIBRARY=alpha
fi

if [ $LIBRARY == alpha ]
then
    MAINACTIVITYSRC='app/alpha'
    PRODUCTION='0zDBBoitw6Un8kR5zPmKZ1XlSgTx4ksvOXqog'
    STAGING='jH0mcUdm0kzlmVMCfaQskiaSPatq4ksvOXqog'
    JPUSHAPPKEY=8c78848e28ae168b4944ecad
    FILESERVICE=http://192.168.1.31:9998
    APISERVICE=http://192.168.1.22:8899
    THISID=app.alpha
    APPNAME=efos-alpha-$THISVERSION

elif [ $LIBRARY == beta ] 
then
    MAINACTIVITYSRC='app/beta'
    PRODUCTION='RFrs34yf8ieTnMeN7tEAAt4xPgrI4ksvOXqog'
    STAGING='T4Lh41UbptUAn81mJNb3CDPJHch14ksvOXqog'
    JPUSHAPPKEY=c405496fa5bebbc30e1c9e2f
    FILESERVICE=http://file.eegrid.com
    APISERVICE=http://test.api.eegrid.com
    THISID=app.beta
    APPNAME=efos-beta-$THISVERSION
else
    MAINACTIVITYSRC='app'
    PRODUCTION='9Rp3Xd2reAnSSbBTrKPXzSXHefdE4ksvOXqog'
    STAGING='B1oStLOhZpVss52H9Md3u9cSBJqC4ksvOXqog'
    JPUSHAPPKEY=8124f747acdcb495b6433d76
    FILESERVICE=http://file.eegrid.com
    APISERVICE=http://clientapi.eegrid.com
    THISID=app
    APPNAME=efos-prod-$THISVERSION
fi

# 更新config.xml版本和版本号
echo "更新config.xml版本和版本号"
    sed -ig -e '2 c <widget id="com.eegrid.efos.'$THISID'" version="'$THISVERSION'" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
' ./config.xml

# 修改config.xml code-push key
echo "修改config.xml code-push key"
codepushline=$(grep -n 'CodePushDeploymentKey' ./config.xml | head -1  | cut  -d  ":"  -f  1)
sed -ig -e "$codepushline c <preference name='CodePushDeploymentKey' value='$PRODUCTION' />
    " ./config.xml

# 安装依赖并打包
rm -f package-lock.json

# 修改code-push-key
echo "修改codepushkey:$PRODUCTION,$STAGING"
sed -ig -e "14 c Production: '$PRODUCTION',
    " -e "15 c Staging: '$STAGING'
    " ./src/app/codepush.constants.ts

# 修改config.ts
echo "修改config.ts"
fileService=$(grep -n 'serviceAddr' ./src/app/services/config.ts | head -1  | cut  -d  ":"  -f  1)
apiService=$(grep -n 'serviceAddr' ./src/app/services/config.ts | tail -1  | cut  -d  ":"  -f  1)

 sed -ig -e "$fileService c 'serviceAddr': '$FILESERVICE',
    " -e "$apiService c 'serviceAddr': '$APISERVICE',
" ./src/app/services/config.ts

ionic cordova plugin rm jpush-phonegap-plugin  --variable APP_KEY=8c78848e28ae168b4944ecad
ionic cordova plugin add jpush-phonegap-plugin --variable APP_KEY=$JPUSHAPPKEY

ionic cordova platform rm android
ionic cordova platform add android@7.1.4 --nofetch --no-resources

# 修改平台--MainActivity.java
echo '修改平台文件--MainActivity.java'
sed -ig -e "23 a import android.os.Build;
" -e "23 a import android.view.View;
" -e "29 a getWindow().getDecorView().setSystemUiVisibility(
" -e "29 a View.SYSTEM_UI_FLAG_FULLSCREEN| View.SYSTEM_UI_FLAG_LAYOUT_STABLE| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_IMMERSIVE
" -e "29 a );
" ./platforms/android/app/src/main/java/com/eegrid/efos/$MAINACTIVITYSRC/MainActivity.java

#修改平台--AndroidManifest.xml
echo '修改平台文件--AndroidManifest.xml'
sed -ig -e 's/package="com.eegrid/android:resizeableActivity="true" package="com.eegrid/
' -e '5 a <meta-data android:name="android.max_aspect" android:value="2.5" />
' -e '5 a <meta-data android:name="android.notch_support" android:value="true" />
' ./platforms/android/app/src/main/AndroidManifest.xml

#修改图标和起始页
cp ./sh/icon/$LIBRARY.png ./resources/icon.png
ionic cordova resources
